//
//  QualitySortTableViewCell.m
//  Beauty
//
//  Created by Snow on 16/9/10.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "QualitySortTableViewCell.h"
#import "QualitySortCollectionViewCell.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfSortFind.h"

@interface QualitySortTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *arrayOfSort;

@end
@implementation QualitySortTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self handleData];
        self.contentView.layer.masksToBounds = YES;
        self.contentView.layer.cornerRadius = 5;
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.minimumLineSpacing = 5;
        _layout.minimumInteritemSpacing = 5;
        _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
//        _collectionView.bounces = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.contentView addSubview:_collectionView];
        [_collectionView registerClass:[QualitySortCollectionViewCell class] forCellWithReuseIdentifier:@"QualitySortCollectionViewCell"];
    }
    return self;
}
- (void)handleData
{
    _arrayOfSort = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=goods_class&op=index&gc_id=1535" par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        NSArray *arr = dic[@"class_list"];
        for (NSDictionary *dictionary in arr) {
            ModelOfSortFind *model = [[ModelOfSortFind alloc] init];
            [model setValuesForKeysWithDictionary:dictionary];
            [_arrayOfSort addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake((self.contentView.bounds.size.width - 20) / 3, self.contentView.bounds.size.height - 10);
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right);
        make.top.mas_equalTo(self.contentView.mas_top);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfSort.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    QualitySortCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QualitySortCollectionViewCell" forIndexPath:indexPath];
    ModelOfSortFind *model = [_arrayOfSort objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.image]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfSortFind *model = [_arrayOfSort objectAtIndex:indexPath.row];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(sendGc_Id:)]) {
        [self.delegate sendGc_Id:model.gc_id];
    }
}
@end
